import Vue from 'vue';
import Logan from 'logan-web';
import { ToastPlugin, LoadingPlugin } from 'vux';
import store from '@/store';
import i18n from '@/common/i18n';

Vue.use(ToastPlugin);
Vue.use(LoadingPlugin);

const host = window.location.host;
Logan.initConfig({
  reportUrl: `${window.location.protocol}//${host}/app-template/api/logs/web/upload`,
  logTryTimes: 3,
  dbName: 'logan_web_db',
  errorHandler: function(e) {},
});

const logType = {
  INFO: 1, // INFO
  WARN: 2, // WARN
  ERROR: 3, // ERROR
};

const logan = {
  log(content, isEncrypt = false) {
    console.log('take notes logan', content);
    if (isEncrypt) {
      Logan.logWithEncryption(content, logType.INFO);
    } else {
      Logan.log(content, logType.INFO);
    }
  },
  warn(content, isEncrypt = false) {
    console.log('take notes logan', content);
    if (isEncrypt) {
      Logan.logWithEncryption(content, logType.WARN);
    } else {
      Logan.log(content, logType.WARN);
    }
  },
  error(content, isEncrypt = false) {
    console.log('take notes logan', content);
    if (isEncrypt) {
      Logan.logWithEncryption(content, logType.ERROR);
    } else {
      Logan.log(content, logType.ERROR);
    }
  },
  report(fromDate, toDate) {
    vrv.jssdk.getClientInfo({
      success: async(client) => {
        const me = store.state.me;
        const customInfo = {
          userId: me.user.sourceId,
          username: me.user.displayName,
        };
        Vue.$vux.loading.show({
          text: i18n.t('common.uploading'),
        });
        try {
          const reportResult = await Logan.report({
            deviceId: client.mac,
            webSource: client.deviceModel,
            environment: window.navigator.userAgent,
            fromDayString: fromDate,
            toDayString: toDate,
            customInfo: JSON.stringify(customInfo),
          });

          let result = '';
          for (const key in reportResult) {
            if (reportResult[key].msg === 'Report fail') {
              result = 'fail';
              break;
            }
            if (reportResult[key].msg === 'Report succ') {
              result = 'succ';
              break;
            }
            if (reportResult[key].msg === 'No log exists') {
              result = 'exists';
            }
          }
          if (result === 'succ') {
            Vue.$vux.toast.show({
              text: i18n.t('setting.uploadSucceeded'),
              time: 2000,
            });
          } else if (result === 'fail') {
            Vue.$vux.toast.show({
              type: 'warn',
              text: i18n.t('setting.uploadFailed'),
              time: 2000,
            });
          } else if (result === 'exists') {
            Vue.$vux.toast.show({
              type: 'text',
              text: i18n.t('setting.uploadNoExists'),
              time: 2000,
            });
          }
          console.log(reportResult); // for debug
        } catch (error) {
          Vue.$vux.toast.show({
            type: 'warn',
            text: i18n.t('setting.uploadFailed'),
            time: 2000,
          });
          console.error('logan report error', error);
        } finally {
          Vue.$vux.loading.hide();
        }
      },
    });
  },
};

export default logan;
